import 'dart:async';

import 'package:get/get.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/pages/lf_home/lf_home_logic.dart';

import '../../entity/entities/lf_user_entity.dart';
import '../../events/delete_badge_count.dart';
import '../../events/payment_success_event.dart';
import '../../events/user_relation_event.dart';
import '../../service/manager/lf_event_helper.dart';
import '../../service/lf_auth_service.dart';

enum ContactType { likeMe, visitMe, myFave }

abstract class LfContactSubLogic extends LfBaseListController<LfUserEntity> {
  LfAuthService authService = Get.find();
  StreamSubscription? relationSubscription,
      deleteActionSubscription,
      streamSubscriptionPaymentSuccess;
  ContactType pageType();
  @override
  void onReady() {
    super.onReady();
    relationSubscription =
        LfEventHelper().getEvent().on<UserRelationEvent>().listen((event) {
      if (event.type == UserRelationType.block) {
        deleteBadgeAndEntity(event.uid);
        if (dataList.isEmpty) {
          getCustomParamHttpRequest(isRefresh: true);
        } else {
          update();
        }
      } else if (event.type == UserRelationType.like) {
        if (pageType() == ContactType.myFave) {
          if (event.userEntity != null) {
            event.userEntity!.liked = 1;
            dataList.insert(0, event.userEntity!);
            update();
          }
        } else {
          var entity = dataList
              .firstWhereOrNull((element) => element.userId == event.uid);
          if (entity != null) {
            entity.liked = 1;
            update();
          }
        }
      } else if (event.type == UserRelationType.unlike) {
        if (pageType() == ContactType.myFave) {
          dataList.removeWhere((element) => element.userId == event.uid);
          if (dataList.isEmpty) {
            getCustomParamHttpRequest(isRefresh: true);
          } else {
            update();
          }
        } else {
          var entity = dataList
              .firstWhereOrNull((element) => element.userId == event.uid);
          if (entity != null) {
            entity.liked = 0;
            update();
          }
        }
      } else if (event.type == UserRelationType.wink) {
        var entity =
            dataList.firstWhereOrNull((element) => element.userId == event.uid);
        if (entity != null) {
          entity.winked = 1;
          update();
        }
      } else if (event.type == UserRelationType.requestPrivate) {
        var entity =
            dataList.firstWhereOrNull((element) => element.userId == event.uid);
        if (entity != null) {
          entity.privateAlbumStatus = 2;
          update();
        }
      }
    });

    deleteActionSubscription =
        LfEventHelper().getEvent().on<DeleteBadgeCountEvent>().listen((event) {
      if (pageType() == ContactType.myFave) {
        return;
      }
      if (event.type == BadgeChangeType.likedMe &&
          pageType() == ContactType.likeMe) {
        var entity =
            dataList.firstWhereOrNull((element) => element.userId == event.uid);
        if (entity != null && entity.isNew > 0) {
          LfHomeLogic logic = Get.find();
          logic.changeValueBadges(
              type: BadgeChangeType.likedMe,
              value: logic.badgeEntity.value.newLikedMe - entity.isNew);
          entity.isNew = 0;
          update();
        }
      } else if (event.type == BadgeChangeType.visitedMe &&
          pageType() == ContactType.visitMe) {
        var entity =
            dataList.firstWhereOrNull((element) => element.userId == event.uid);
        if (entity != null && entity.isNew > 0) {
          LfHomeLogic logic = Get.find();
          logic.changeValueBadges(
              type: BadgeChangeType.visitedMe,
              value: logic.badgeEntity.value.newVisitedMe - entity.isNew);
          entity.isNew = 0;
          update();
        }
      }
    });
    streamSubscriptionPaymentSuccess =
        LfEventHelper().getEvent().on<PaymentSuccessEvent>().listen((event) {
      update();
    });
  }

  void deleteBadgeAndEntity(String uid) {
    if (pageType() == ContactType.myFave) {
      dataList.removeWhere((element) => element.userId == uid);
      return;

      ///这个页面没红点,所以不用管
    }
    var entity = dataList.firstWhereOrNull((element) => element.userId == uid);
    if (entity != null) {
      if (entity.isNew > 0) {
        LfHomeLogic logic = Get.find();
        if (pageType() == ContactType.visitMe) {
          logic.changeValueBadges(
              type: BadgeChangeType.visitedMe,
              value: logic.badgeEntity.value.newVisitedMe - entity.isNew);
        } else if (pageType() == ContactType.likeMe) {
          logic.changeValueBadges(
              type: BadgeChangeType.likedMe,
              value: logic.badgeEntity.value.newLikedMe - entity.isNew);
        }
      }
      dataList.removeWhere((element) => element.userId == uid);
    }
  }

  @override
  void onClose() {
    relationSubscription?.cancel();
    streamSubscriptionPaymentSuccess?.cancel();
    deleteActionSubscription?.cancel();
    super.onClose();
  }

  @override
  bool filterKey({required LfUserEntity value, required LfUserEntity other}) {
    return value.userId == other.userId;
  }
}
